/*
 * Copyright 2010 Pierre Heinzerling, Uwe Janner, Patrick Wright
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.upp.publinksky.dao;

import com.upp.publinksky.model.Theme;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 */
@SuppressWarnings({"unchecked"})
public class ThemeDAO {
    public static Theme insertTheme(final String name, final String email, final PersistenceManager pm) {
        Theme newTheme = new Theme();
        newTheme.setName(name);
        newTheme.setCreatedOn(new Date());
        newTheme.setOwnerEmail(email);
        newTheme.setMaxScore(1);
        newTheme.setLastUpdateCounter(0);
        newTheme.setLastUpdateByEmail(email);
        pm.makePersistent(newTheme);
        return newTheme;
    }

    public static Theme getThemeScratchPad(final String email, final PersistenceManager pm) {
        Query qry = pm.newQuery(Theme.class);
        qry.setFilter("name == nm && ownerEmail == em");
        qry.declareParameters("String nm, String em");

        List<Theme> res = (List<Theme>) qry.execute(Theme.SHARE_NAME_SCRATCHPAD, email);
        return res.isEmpty() ? null : res.get(0);
    }

    public static Theme insertNewScratchPadTheme(final String email, final PersistenceManager pm) {
        final Theme theme = Theme.newScratchPad(email);
        pm.makePersistent(theme);
        return theme;
    }

    public static Collection<? extends Theme> listThemesByOwner(final String email, final PersistenceManager pm) {
        Query qry = pm.newQuery(Theme.class);
        qry.setFilter("ownerEmail == em");
        qry.declareParameters("String em");
        return (Collection<? extends Theme>) qry.execute(email);
    }

    public static Theme getThemeById(final long id, final PersistenceManager pm) {
        try {
            return pm.getObjectById(Theme.class, id);
        } catch (JDOObjectNotFoundException e) {
            return null;
        }
    }

    public static void markUpdatedBy(final Theme theme, final String byEmail, final PersistenceManager pm) {
        theme.markUpdated(byEmail);
        pm.makePersistent(theme);
    }

    public static void incrementMaxScore(final Theme theme, final PersistenceManager pm) {
        changeMaxScore(theme, 1, pm);
    }

    public static void decrementMaxScore(final Theme theme, final PersistenceManager pm) {
        changeMaxScore(theme, -1, pm);
    }

    private static void changeMaxScore(final Theme theme, final int howMuch, final PersistenceManager pm) {
        theme.setMaxScore(theme.getMaxScore() + howMuch);
        pm.makePersistent(theme);
    }
}
